package yl.hs.bmipfull.controllers.laboratory;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import yl.hs.bmipfull.controllers.ControllerBase;
import yl.hs.bmipfull.service.laboratory.IProductInfoService;
import yl.hs.bmipfull.service.system.ISearchViewService;
import yl.hs.bmipfull.utils.Tl;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.SubmitModel;
import yl.hs.bmipfull.viewmodels.WebResult;

import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 作用:  产品信息表控制层<br/>
 * 创建时间: 2021年05月22日 14时48分28秒 <br/>
 *
 * @author 吴高丰
 */
@Api(value = "产品信息表Web接口", tags = "实验室管理——产品信息表操作")
@RequestMapping(value = "/api/product")
@RestController
public class ProductInfoController extends ControllerBase {

    @Autowired
    IProductInfoService service;
    @Autowired
    ISearchViewService svSVC;

    @ApiOperation(value = "查询产品信息表", notes = "查询产品信息表")
    @RequestMapping(value = "/info", method = RequestMethod.POST)
    public WebResult queryList(@ApiParam(value = "查询条件") @RequestBody PageQuery pageQuery) {
        try {
            return success("查询成功", service.queryList(pageQuery));
        } catch (Exception e) {
            return error(e);
        }
    }

    @ApiOperation(value = "查询产品信息表", notes = "查询产品信息表")
    @RequestMapping(value = {"/select", "/select/{cateSN}"}, method = RequestMethod.GET)
    public WebResult querySelectList(@PathVariable(required = false, name = "cateSN") String cateSN) {
        try {
            var dataList = service.queryByCateSn(cateSN);

            var newData = dataList.stream().map(t -> {
                Map<String, String> item = new HashMap<>();
                item.put("value", t.getSn());
                var label = new StringBuffer(t.getProductName());
                if (!Tl.isEmpty(t.getProductSpec())) {
                    label.append("【");
                    label.append(t.getProductSpec());
                    label.append("】");
                }
                item.put("productSpec",t.getProductSpec());
                item.put("label", label.toString());
                return item;
            });

            return success("查询成功", newData.collect(Collectors.toList()));
        } catch (Exception e) {
            return error(e);
        }
    }

    @ApiOperation(value = "查询产品信息表", notes = "分页查询产品信息表")
    @RequestMapping(value = "/info/{rowNum}/{page}", method = RequestMethod.POST)
    public WebResult queryPage(@RequestBody PageQuery query,
                               @ApiParam(value = "页码") @PathVariable int page,
                               @ApiParam(value = "每页条数") @PathVariable int rowNum) {
        try {
            var result = service.queryPage(query, rowNum, page);
            svSVC.updateUserMenuView(query);
            return success("ok", result);
        } catch (Exception ee) {
            return error(ee);
        }
    }


    @ApiOperation(value = "查询产品信息表", notes = "根据产品信息表编号查询产品信息表")
    @RequestMapping(value = "/info/{sn}", method = RequestMethod.GET)
    public WebResult findBySn(@ApiParam(value = "编号") @PathVariable String sn) {
        return success("查询成功", service.findBySn(sn));
    }

    @ApiOperation(value = "查询产品信息表", notes = "根据产品规格编号查询产品信息表")
    @RequestMapping(value = "/cateProducts/{cateSN}", method = RequestMethod.GET)
    public WebResult findByTags(@ApiParam(value = "编号") @PathVariable String cateSN) {
        return success("查询成功", service.queryByTags(cateSN));
    }

    @ApiOperation(value = "保存产品信息表", notes = "查询/更新产品信息表信息")
    @RequestMapping(value = "/info", method = RequestMethod.PUT)
    public WebResult saveMenu(@ApiParam(value = "提交信息") @RequestBody SubmitModel submitModel) {
        Integer result;
        try {
            result = service.saveProductInfo(submitModel.getData(), submitModel.getSn());
            if (result > 0) {
                return success("保存成功");
            } else {
                return fail("数据未保存");
            }
        } catch (Exception e) {
            return error(e);
        }

    }

    @ApiOperation(value = "删除产品信息表信息", notes = "根据产品信息表编号删除系产品信息表信息")
    @RequestMapping(path = "/info", method = RequestMethod.DELETE)
    public WebResult delete(@ApiParam(value = "产品信息表编号", name = "sn", allowMultiple = true, required = true) @RequestParam("sn") String[] sn) {
        if (service.delete(sn) > 0) {
            return success("删除成功");
        } else {
            return fail("删除失败");
        }
    }

    @ApiOperation(value = "获取产品容重", notes = "根据产品编号和合同编号，获取产品容重")
    @RequestMapping(path = "/info/density", method = RequestMethod.GET)
    public WebResult getDensity(@ApiParam(value = "合同编号") @RequestParam String contractSn,
                                @ApiParam(value = "产品编号") @RequestParam String productSn) {
        try {
            return success("成功", service.getDensity(contractSn, productSn));
        } catch (Exception e) {

            return error("失败", e);
        }
    }

    @ApiOperation(value = "获取产品容重", notes = "根据产品编号和合同编号，获取产品容重")
    @RequestMapping(path = "/info/product/quryall", method = RequestMethod.GET)
    public WebResult getProductAll() {
        try {
            return success("成功", service.queryAll());
        } catch (Exception e) {
            return error(e);
        }
    }

    @ApiOperation(value = "根据产品编号修改状态", notes = "根据产品编号修改发货单状态")
    @RequestMapping(value = "/info/changeStateBySn/{sn}/{state}", method = RequestMethod.GET)
    @ResponseBody
    public WebResult changeStateBySn(@PathVariable String sn,@PathVariable short state) {
        try {
            Integer result = service.changeStateBySN(sn.split(","), state);
            if (result > 0) {
                return success("数据已修改");
            } else if (result == -2) {
                return fail("数据修改失败！");
            }
            return fail("没有修改的任何数据");
        } catch (Exception ee) {
            return error(ee);
        }
    }
}